<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>编辑客户 - 电动车销售系统</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/css/purchase.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/css/admin/updatecustomer.css">
</head>
<body>
<div class="container py-5">
    <div class="row justify-content-center">
        <div class="col-lg-8">
            <div class="card shadow-lg">
                <div class="card-header py-3 d-flex justify-content-between align-items-center">
                    <h2 class="h4 m-0"><i class="bi bi-person-gear me-2"></i>编辑客户信息</h2>
                    <div>
                        <span class="badge bg-primary">
                            <i class="bi bi-person-circle me-1"></i> 系统管理员
                        </span>
                    </div>
                </div>

                <div class="card-body p-4">
                    <c:if test="${not empty errorMsg}">
                        <div class="alert alert-danger alert-dismissible fade show mb-4" role="alert">
                            <i class="bi bi-exclamation-triangle me-2"></i> ${errorMsg}
                            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                        </div>
                        <c:remove var="errorMsg" scope="session"/>
                    </c:if>

                    <c:if test="${not empty customer}">
                        <div class="text-center mb-5">
                            <div class="customer-avatar">
                                    ${fn:substring(customer.name, 0, 1)}
                            </div>
                            <h3 class="mb-1">${customer.name}</h3>
                            <p class="text-muted">客户ID: #${customer.id}</p>
                        </div>

                        <form action="${pageContext.request.contextPath}/UpdateCustomerServlet" method="post">
                            <input type="hidden" name="id" value="${customer.id}">

                            <div class="mb-4">
                                <div class="row">
                                    <div class="col-md-6 mb-3">
                                        <label class="form-label">客户姓名</label>
                                        <input type="text" class="form-control" name="name" value="${customer.name}" required>
                                    </div>
                                    <div class="col-md-6 mb-3">
                                        <label class="form-label">联系电话</label>
                                        <input type="tel" class="form-control" name="phone" value="${customer.phone}" required>
                                    </div>
                                </div>

                                <div class="mb-4">
                                    <label class="form-label">联系地址</label>
                                    <textarea class="form-control" name="address" rows="3" required>${customer.address}</textarea>
                                </div>

                                <div class="info-card mb-4">
                                    <h5><i class="bi bi-info-circle me-2"></i>客户信息</h5>
                                    <div class="d-flex justify-content-between mt-3">
                                        <span class="text-muted">注册时间：</span>
                                        <span><fmt:formatDate value="${customer.create_time}" pattern="yyyy-MM-dd HH:mm"/></span>
                                    </div>
                                </div>
                            </div>

                            <div class="d-flex justify-content-between mt-5">
                                <a href="${pageContext.request.contextPath}/CustomerListServlet" class="btn btn-outline-secondary">
                                    <i class="bi bi-arrow-left me-1"></i> 返回列表
                                </a>
                                <button type="submit" class="btn btn-primary">
                                    <i class="bi bi-check-circle me-1"></i> 保存更改
                                </button>
                            </div>
                        </form>
                    </c:if>

                    <c:if test="${empty customer}">
                        <div class="text-center py-5">
                            <div class="py-5">
                                <i class="bi bi-exclamation-circle text-danger" style="font-size: 4rem;"></i>
                                <h3 class="mt-4">未找到客户信息</h3>
                                <p class="text-muted mb-4">您尝试编辑的客户不存在或已被删除</p>
                                <a href="${pageContext.request.contextPath}/CustomerListServlet" class="btn btn-primary">
                                    <i class="bi bi-people me-1"></i> 返回客户列表
                                </a>
                            </div>
                        </div>
                    </c:if>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script>
    // 自动关闭消息提示
    setTimeout(() => {
        document.querySelectorAll('.alert').forEach(alert => {
            alert.style.display = 'none';
        });
    }, 5000);

    // 表单验证
    document.querySelector('form').addEventListener('submit', function(e) {
        const nameInput = document.querySelector('input[name="name"]');
        const phoneInput = document.querySelector('input[name="phone"]');
        const addressInput = document.querySelector('textarea[name="address"]');

        if (nameInput.value.trim() === '') {
            e.preventDefault();
            alert('请输入客户姓名');
            nameInput.focus();
            return;
        }

        if (phoneInput.value.trim() === '') {
            e.preventDefault();
            alert('请输入联系电话');
            phoneInput.focus();
            return;
        }

        if (addressInput.value.trim() === '') {
            e.preventDefault();
            alert('请输入联系地址');
            addressInput.focus();
            return;
        }
    });
</script>
</body>
</html>